<?php

namespace App\Http\Controllers\Dynamic;

use App\Http\Controllers\ApiController;
use App\Models\Dynamic\SamReferGlucose;
use App\User;
use Carbon\Carbon;
use Illuminate\Http\Request;

class SamReferGlucoseController extends ApiController
{
    /**
     * @var \App\Models\Dynamic\SamReferGlucose
     */

    protected $user;

    protected $samReferGlucose;

    protected $request;


    /**
     * SamReferGlucoseController constructor.
     * @param SamReferGlucose $samReferGlucose
     * @param Request $request
     */
    public function __construct(User $user, SamReferGlucose $samReferGlucose,
                                Request $request)
    {
        parent::__construct();

        $this->user = $user;
        $this->samReferGlucose = $samReferGlucose;
        $this->request = $request;
    }

    /**
     * SamReferGlucoseController Store.
     * @return \Illuminate\Http\JsonResponse
     */

    public function Store()
    {

        $this->validate($this->request, [
            'collectedtime' => 'string|required',
            'value' => 'numeric |min:0',
            'user_id' => 'required|integer'
        ]);


        $user_id = $this->request->input('user_id');
        $collectedtime = $this->request->input('collectedtime');
        $value = $this->request->input('value');

        $user = $this->user
            ->withoutGlobalScopes()
            ->select('id', 'account')
            ->find($user_id);
        if (!$user) {
            return $this->response();
        }

        $samReferGlucose = $this->samReferGlucose
            ->firstOrNew(['userid' => $user_id, 'collectedtime' => $collectedtime]);

        $samReferGlucose->value = $value;
        $samReferGlucose->createdtime = Carbon::now()->toDateTimeString();
        $samReferGlucose->save();


        return $this->response(['id' => $this->samReferGlucose->id]);

    }

    /**
     * @return bool|\Illuminate\Http\JsonResponse
     */
    public function getPictureList()
    {

        $this->validate($this->request, [
            'startTime' => 'string',
            'endTime' => 'string',
            'user_id' => 'required|integer',
        ]);

        $user_id = $this->request->input('user_id');
        $startTime = $this->request->input('startTime');
        $endTime = $this->request->input('endTime');

        //获取参比
        $referGlucose = $this->samReferGlucose
            ->where('userid', '=', $user_id)
            ->whereBetween('collectedtime', [$startTime, $endTime])
            ->orderBy('id', 'desc')
            ->get()
            ->toArray();


        if (!$referGlucose) {
            return $this->response($referGlucose);
        }

        $newReferGlucose = [];
        if ($referGlucose) {
            foreach ($referGlucose as $rk => $rv) {
                $newReferGlucose[$rk] = array(
                    'x' => (strtotime($rv['collectedtime']) + (8 * 60 * 60)) * 1000,
                    'y' => (float)$rv['value'],
                    'attr' => (float)$rv['value'] . "(mmol/L)",
                    'marker' => ['symbol' => 'circle']
                );
            }

        }
        return $this->response($newReferGlucose);
    }
}
